﻿#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using InsuranceAdjuster.Web.Services;
#endregion

namespace InsuranceAdjuster.OccasionallyConnected.Messages {
    public class SyncAppointmentsMessage : Message {

        public SyncAppointmentsMessage() {
            this.CacheRules = MessageDataSource.ServerOnly;
            GroupName = "Activity Sync";
            MessageName = "Sync Appointments";
        }

        protected override void OnlineOperation() {
            MarkAsyncStarted(2);
            GetDataService().GetAdjusterAppointmentsDeltaInformation(entities =>
            {
                if (entities != null && entities.Count > 0) {
                    for (int x = 0; x < entities.Count; x++) {

                        if (entities[x].Value == EntitySyncOperation.Add)
                            ResponseEntities.Add("A" + x.ToString(),  
                                                                    new MessageResult(
                                                                        Verb.Read, 
                                                                        Origin.Server, 
                                                                        entities[x].Key));

                        else if (entities[x].Value == EntitySyncOperation.Update)
                            ResponseEntities.Add("U" + x.ToString(), 
                                                                    new MessageResult(
                                                                        Verb.Update, 
                                                                        Origin.Server, 
                                                                        entities[x].Key));

                        else if (entities[x].Value == EntitySyncOperation.Delete)
                            ResponseEntities.Add("D" + x.ToString(), 
                                                                    new MessageResult(
                                                                        Verb.Delete, 
                                                                        Origin.Server, 
                                                                        entities[x].Key));
                    }
                }

                MarkAsyncFinished();
            });
        }

        #region Other Stuff
        private List<MessageResult> _Results = new List<MessageResult>();
        public override void Clear() {
            base.Clear();
            _Results = null;
        }
        #endregion
    }
}
